package com.metadata_system.qt_client.service;

import com.metadata_system.common.db.entity.Project;
import com.metadata_system.common.db.mapper.DatabaseMapper;
import com.metadata_system.common.db.mapper.ProjectMapper;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.dto.SelectItem;
import com.metadata_system.common.enums.AllConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service("qtClientProject")
public class ProjectService {

    @Autowired(required = false)
    DatabaseMapper databaseMapper;
    @Autowired(required = false)
    ProjectMapper projectMapper;


    public ApiResult getAllList(Integer userId) {
        Example example = new Example(Project.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("find_in_set(user_id, " + userId + ")");
        List<Project> projectList = this.projectMapper.selectByExample(example);

        // 筛选-数据库分类列表,数据库列表
        List<SelectItem> typeList = new ArrayList<>();
        List<SelectItem> databaseList = new ArrayList<>();
        List<Integer> isAddType = new ArrayList<>();
        List<Integer> isAddDataBaseList = new ArrayList<>();
        for (Project project : projectList) {
            for (int i = 0; i < 3; i++) {
                if (!isAddType.contains(i)) {
                    if (project.getType().equals(i)) {
                        typeList.add(SelectItem.builder()
                                .label(AllConst.DatabaseTypeList.get(project.getType()))
                                .value(project.getType())
                                .children(new ArrayList<>())
                                .build());
                        isAddType.add(i);
                    }
                }
            }
            if (!isAddDataBaseList.contains(project.getDatabaseId())) {
                databaseList.add(SelectItem.builder()
                        .label(project.getDatabaseName())
                        .value(project.getDatabaseId())
                        .children(new ArrayList<>())
                        .parentLabel(AllConst.DatabaseTypeList.get(project.getType()))
                        .parentValue(project.getType())
                        .build());
                isAddDataBaseList.add(project.getDatabaseId());
            }
        }


        // 筛选-数据库分类列表,数据库列表  合并
        for (Project project : projectList) {
            for (SelectItem database : databaseList) {
                if (database.getValue().equals(project.getDatabaseId())) {
                    database.getChildren().add(SelectItem.builder()
                            .label(project.getName())
                            .value(project.getId())
                            .parentLabel(project.getDatabaseName())
                            .parentValue(project.getDatabaseId())
                            .children(new ArrayList<>())
                            .build());
                }
            }
        }


        for (SelectItem database : databaseList) {
            for (SelectItem selectItem : typeList) {
                if (database.getParentValue().equals(selectItem.getValue())) {
                    selectItem.getChildren().add(database);
                }
            }
        }

        return ApiResult.success(typeList);
    }
}
